const app = getApp(),util=require('../../utils/util.js');
Page({
  data:{
    canIUse:wx.canIUse('button.open-type.getUserInfo'),
    userInfo: {},
    userData:{},
    hasUserInfo: false,
    statusText:'',
    type:'1',
    why:false,
    reason:'',
  },
  // onLoad: function() {
  //   // 查看是否授权
  //   wx.getSetting({
  //     success (res){
  //       if (res.authSetting['scope.userInfo']) {
  //         // 已经授权，可以直接调用 getUserProfile 获取头像昵称
  //         wx.getUserProfile({
  //           success: function(res) {
  //             userInfo
  //             console.log(res.userInfo)
  //           }
  //         })
  //       }
  //     }
  //   })
  // },
  onShow(){
    wx.removeStorageSync('type')
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true,
        why:false
      })
      this.getUserData()
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
        this.getUserData()
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
          this.getUserData()

        }
      })
    }
  },
  toBank(){
    wx.navigateTo({
      url: '../../pages/bank/bank',
    })
  },
  getUserData(){
    let that=this,statusText='',type='';
    this.setData({
      userData:app.globalData.userData
    })
    util.post('users/userData',{},function(res){
      console.log(res,'成功回调')
      let status=res.data.data.is_cert;
      if(status==1){
        statusText='未认证'
        type='1'
      }else if(status==2){
        statusText='已认证'
        type='2'
      }else if(status==3){
        statusText='待审核'
        type='1'
      }else if(status==4){
        statusText='未通过认证'
        type='3'
      }
      that.setData({
        statusText,
        type
      })
      wx.setStorageSync('type', type)
    })
   
  },
  cancel(){
    this.setData({
      why:false
    })
  },
  why(){
    let that = this
    util.post('users/userData',{},function(res){
      console.log(res)
      that.setData({
        why:true,
        reason:res.data.data.remarks
      })
    })
  },
  loginAgain(){
wx.navigateTo({
  url: '../authentication/authentication',
})
  },
  getSetting(){
    let that=this;
    //获取用户信息
    //wx.getUserInfo即将不支持,我用了新的请求方式
    //以下是获取的一种
          wx.getUserProfile({
            desc: "获取你的昵称、头像、地区及性别",
            success:res=>{
              app.globalData.userInfo = res.userInfo
              app.globalData.hasUserInfo = true,
              that.login(res.userInfo)
              //console.log(app.globalData.userInfo,'用户信息')
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (that.userInfoReadyCallback) {
                that.userInfoReadyCallback(res)
              }
            },fail:res=>{
                  console.log(res,'获取授权信息失败')
                }
          });
   
  },
  login(userInfo){
    let that=this;
    // 登录
    wx.login({
     
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        console.log(res)
        wx.request({
          url: app.globalData.url+'users/wxlogin',
          data:{code:res.code},
          method:'POST',
          success(e){
            console.log(e,'登录内容')
            if(e.data.code==1){
              app.globalData.userData=e.data.data
              that.onShow()
              wx.setStorageSync('userData', e.data.data)
            }else if(e.data.code==2){
              that.regist(e.data.data.openid,userInfo)
            }
          },
          fail(e){}
        })
      }
    })
  },
  regist(openid,reg){
    let that=this,regData=reg;
    regData.openid=openid;
    wx.request({
      url: app.globalData.url+'users/reg',
      data:regData,
      method:'POST',
      success(res){
        console.log(res,'用户信息')
        app.globalData.userData=res.data.data
      },
      fail(res){
        console.log(res,'用户信息请求失败')
      }
    })
  },
  getUserInfo(){
    wx.getUserInfo({
      withCredentials:true,
      success(res){
        console.log(res,'用户信息')
      },
      fail(res){
        console.log(res,'获取用户信息失败')
      }
    })
  },
  handleWithd(){
    wx.navigateTo({
      url: '../withdrawal/withdrawal',
    })
  },
  handleTorenz(){
     if (wx.getStorageSync('type')==2){
      wx.showToast({
        icon:'none',
        title:'您已认证成功'
      })
    }else{
      wx.navigateTo({
        url: '../authentication/authentication',
      })
    }
    
  },
  toProfit(){
    wx.navigateTo({
      url: '../profit/profit',
    })
  },
  minerep(){
    wx.navigateTo({
      url: '../minerep/minerep',
    })
  }
})